#include <iostream>
#include "Ringqueue.hpp"
#include "Thread.hpp"

using namespace RingBufferModule;
using namespace ThreadModule;
void *Productor(Ringbuffer<int> &rq)
{
    int data = 10;
    while (true)
    {
        // sleep(2); // 先让消费者进入等待
        printf("生产者生产了一个data: %d\n", data);
        rq.Equeue(data);
        data++;
    }
}
void *Consumer(Ringbuffer<int> &rq)
{
    while (true)
    {
        sleep(1);
        int data = 0;
        rq.Pop(&data);
        printf("consumer get a data: %d\n", data);
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
   Ringbuffer<int> *rq  = new Ringbuffer<int>(10);

    Thread<Ringbuffer<int>> p1(Productor, *rq);
    Thread<Ringbuffer<int>> p2(Productor, *rq);
    Thread<Ringbuffer<int>> c1(Consumer, *rq);
    Thread<Ringbuffer<int>> c2(Consumer, *rq);
    Thread<Ringbuffer<int>> c3(Consumer, *rq);

    p1.Start();
    p2.Start();
    c1.Start();
    c2.Start();
    c3.Start();
    p1.Join();
    p2.Join();
    c1.Join();
    c2.Join();
    c3.Join();
    delete rq;
    return 0;
}

// class ThreadData{
//     public:
//         ThreadData(Ringbuffer<int>* ring,const std::string& name)
//             :_ring(ring),_name(name)
//             {}
//         ~ThreadData(){}
//         Ringbuffer<int>* _ring;
//         std::string _name;
// };
// void *Productor(void*args)
// {
//     ThreadData* rq = static_cast<ThreadData*>(args);
//     int data = 10;
//     while (true)
//     {
//         // sleep(1); // 先让消费者进入等待
//         printf("%s(生产者) 生产了一个data: %d\n", rq->_name.c_str(),data);
//         rq->_ring->Equeue(data);
//         data++;
//     }
// }
// void *Consumer(void*args)
// {
//     ThreadData* rq = static_cast<ThreadData*>(args);    
//     while (true)
//     {
//         sleep(1);
//         int data = 0;
//         rq->_ring->Pop(&data);
//         printf("%s(消费者) get a data: %d\n",  rq->_name.c_str(),data);
//     }
// }

// int main()
// {
//     srand(time(nullptr) ^ getpid());
//     Ringbuffer<int> *rq  = new Ringbuffer<int>(10);
//     pthread_t p1,p2,c1,c2,c3;
    
//     ThreadData* P1 = new ThreadData(rq,"productor1");
//     ThreadData* P2 = new ThreadData(rq,"productor2");
//     ThreadData* C1 = new ThreadData(rq,"Consumer1");
//     ThreadData* C2 = new ThreadData(rq,"Consumer2");
//     ThreadData* C3 = new ThreadData(rq,"Consumer3");
    
//     pthread_create(&p1,nullptr,Productor,(void*)P1);
//     pthread_create(&p2,nullptr,Productor,(void*)P2);
//     pthread_create(&c1,nullptr,Consumer,(void*)C1);
//     pthread_create(&c2,nullptr,Consumer,(void*)C2);
//     pthread_create(&c3,nullptr,Consumer,(void*)C3);


//     pthread_join(p1,nullptr);
//     pthread_join(p2,nullptr);
//     pthread_join(c1,nullptr);
//     pthread_join(c2,nullptr);
//     pthread_join(c3,nullptr);

//     delete rq;
//     return 0;
// }